<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="字典名称" prop="dictType">

				角色id：{{this.queryParams.roleId}}
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['biz:commissionConfig:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['biz:commissionConfig:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['biz:commissionConfig:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['biz:commissionConfig:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="commissionConfigList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="角色" align="center" prop="roleName" />
			<el-table-column label="类型" align="center" prop="typeName" />
			<!-- <el-table-column label="等级" align="center" prop="level" /> -->
			<!-- <el-table-column label="投资金额" align="center" prop="amount" /> -->
			<el-table-column label="提成" align="center" prop="commission" />
			<el-table-column label="二级提成" align="center" prop="commissionSecond" />
			<el-table-column label="上限倍数" align="center" prop="commissionReduceTimes" />
			<el-table-column label="达成投资金额倍数后的提成" align="center" prop="commissionAfterTimesAmount" />
			<el-table-column label="达成投资金额倍数后的二级提成" align="center" prop="commissionSecondAfterTimesAmount" />
			<el-table-column label="备注" align="center" prop="remark" />
			<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:commissionConfig:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:commissionConfig:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改提成配置对话框 -->
		<el-dialog :title="title" :visible.sync="open" :close-on-click-modal='false' width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="200px">
				<el-form-item label="角色id" prop="roleId">
					<el-input v-model="form.roleId" disabled placeholder="请输入角色id" />
				</el-form-item>
				<el-form-item label="类型" prop="type">

					<!-- <el-input v-model="form.type" type="number" placeholder="请输入类型" /> -->
					<el-select v-model="form.type" placeholder="请选择分类" clearable>
						<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="等级" prop="level">
					<el-input v-model="form.level" type="number" placeholder="请输入等级" />
				</el-form-item> -->
				<!-- <el-form-item label="投资金额" prop="amount">
					<el-input v-model="form.amount" type="number" placeholder="请输入投资金额" />
				</el-form-item> -->
				<el-form-item label="提成(%)" prop="commission">
					<el-input v-model="form.commission" type="number" placeholder="请输入提成" />
				</el-form-item>
				<el-form-item label="二级提成(%)" prop="commissionSecond">
					<el-input v-model="form.commissionSecond" type="number" placeholder="请输入二级提成" />
				</el-form-item>
				<el-form-item label="达成投资金额倍数后的提成(%)" prop="commissionAfterTimesAmount">
					<el-input v-model="form.commissionAfterTimesAmount" type="number" placeholder="请输入达成投资金额倍数后的提成" />
				</el-form-item>
				<el-form-item label="达成投资金额倍数后的二级提成(%)" prop="commissionSecondAfterTimesAmount">
					<el-input v-model="form.commissionSecondAfterTimesAmount" type="number" placeholder="请输入达成投资金额倍数后的二级提成" />
				</el-form-item>
				<el-form-item label="上限倍数" prop="commissionReduceTimes">
					<el-input v-model="form.commissionReduceTimes" type="number" placeholder="请输入上限倍数" />
				</el-form-item>

				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {
	listCommissionConfig,
	getCommissionConfig,
	delCommissionConfig,
	addCommissionConfig,
	updateCommissionConfig,
} from '@/api/biz/commissionConfig';

export default {
	name: 'CommissionConfig',
	data() {
		return {
			// <!-- 类型1  2 消费抽奖门店 3会员费 4 招商 5用户购买积分 6广告 7 积分赠送分店 8 用户充值消费补贴金 -->
			typeList: [
				{ name: '回收门店', id: 1 },
				{ name: '消费抽奖门店', id: 2 },
				{ name: '会员费', id: 3 },
				{ name: '招商', id: 4 },
				{ name: '用户购买积分', id: 5 },
				{ name: '广告', id: 6 },
				{ name: '积分赠送分店', id: 7 },
				{ name: '用户充值消费补贴金', id: 8 },
				{ name: '消费补贴提成', id: 9 },
				{ name: '充值消费补贴金提成', id: 10 },
				{ name: '充值抽奖券提成', id: 11 },
				{ name: '自营补贴门店提成', id: 12 },
				{ name: '充值可赠送投资金额提成', id: 13 },
			],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 提成配置表格数据
			commissionConfigList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				type: null,
				roleId: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.queryParams.roleId =
			this.$route.params && this.$route.params.roleId;
		this.getList();
	},
	methods: {
		/** 查询提成配置列表 */
		getList() {
			this.loading = true;
			listCommissionConfig(this.queryParams).then((response) => {
				this.commissionConfigList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				roleId: this.queryParams.roleId,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加提成配置';
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			getCommissionConfig(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '修改提成配置';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateCommissionConfig(this.form).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.open = false;
							this.getList();
						});
					} else {
						addCommissionConfig(this.form).then((response) => {
							this.$modal.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$modal
				.confirm('是否确认删除提成配置编号为"' + ids + '"的数据项？')
				.then(function () {
					return delCommissionConfig(ids);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				'biz/commissionConfig/export',
				{
					...this.queryParams,
				},
				`commissionConfig_${new Date().getTime()}.xlsx`
			);
		},
	},
};
</script>
